﻿//============================================
// filename: main.c
// chip    : CSU18M68
// author  : fang
// date    : 2024-04-09
//============================================
#include "CSU18M68.h"
#include "user_config.h"

#include "framework_ndt_common.h"
#include "interface.h"
#include "ndt_algorithm.h"
#include "pmu.h"
#include "sofrware_param.h"

#include "hal_ndt_common.h"
#include "hal_afe.h"
#include "hal_gpio.h"
#include "hal_temperature.h"
#include "hal_iic.h"
#include "hal_timer.h"
#include "calibration.h"

//============================================
// program start
//============================================


int main()
{

	asm("CLRWDT");                                    //喂狗
	Hal_Common_Init();                                //hal层，硬件初始化    
	Framework_Common_Init();                          //软件层初始化
	User_Param_Init();                                //用户配置(参数配置)初始化
		
	while(1)
	{	
		asm("CLRWDT"); 
		if(gPmuWorkMode != PMU_SHUT_DOWN_MODE)	// 不在shut down 模式时
		{
			AFE_SampleStartOnce();	
			if(gCalibration_flag == 0)
			{
				//算法处理
				PressKey_Proces();	 
			}
			else
			{
				Calibration_Proce(); //内部校准代码
			}	                
		}
		
		Interface_Proc();		// 接口通讯处理	
		PMU_Proc();				// 周期与功耗控制
	}
	
	return 0;
}




//============================================
// interrupt function
//============================================
void INT_FUNCTION(void) interrupt
{
	// I2C 中断处理		
	if(I2C_RIF) //IIC 接受数据处理
	{	
//		I2C_rPointer = 0;
		I2C_RIF = 0;
		I2C_W_Buff = I2CDAT;
			
		if(flag_IntoSleep==0) //iic 正常模式
		{	
			if(flag_IIC_RWAddr == 0) //第一次获取数据，作为地址解析
			{				
				Register = I2C_W_Buff;
				switch(Register)
				{
					case REG_FW_VERSION_CMD:
						asm("goto	Register_equal_10_pro");
					break;
					case REG_DATA_CMD:
						asm("goto	Register_equal_60_pro");
					break;
					case REG_DATA_LEN:
						asm("goto	Register_equal_61_pro");
						break;
					case REG_DATA:
						asm("goto	Register_equal_62_pro");
						break;
					case REG_BUTTON_FORCE_TRI:
						asm("goto	Register_equal_0x5b_pro");
						break;
					case REG_HANDSHAKE:
						asm("goto	Register_equal_0x01_pro");	
					break;
					
					case REG_STATIC_READ_FORCE_CMD:
						asm("goto	Register_equal_0x21_pro");	
					break;
					
					case REG_SYS_RESET_CMD:
						asm("goto	Register_equal_0x02_pro");	
					break;
					
					case REG_READ_OFFSET_CMD:
						asm("goto	Register_equal_0x17_pro");	
					break;
					
					
					case REG_READ_CAL_FACTOR_CMD:
						asm("goto	Register_equal_0x24_pro");	
					break;
					
					
					case REG_READ_ORI_ADC_CMD:
						asm("goto	Register_equal_0x22_pro");	
					break;
					default:
						asm("goto	Register_switch_end");
					break;			
				}
			
						
asm("Register_equal_0_pro:	");
			RegBuff = Reg_WRBuff;  
			I2CDAT = RegBuff[0];		// 提前赋值
			Reg_OP_Mode = REG_OP_READ_ONLY;
			Reg_Num = 4;			
asm("goto	Register_switch_end	");

asm("Register_equal_0x01_pro:	");
			RegBuff = Reg_Handshake;  
			I2CDAT = RegBuff[0];		// 提前赋值
			Reg_OP_Mode = REG_OP_READ_WRITE;
			Reg_Num = 2;			
asm("goto	Register_switch_end	");

asm("Register_equal_0x02_pro:  ");
			RegBuff = Reg_Reset;  
			I2CDAT = RegBuff[0];		// 提前赋值
			Reg_OP_Mode = REG_OP_READ_WRITE;
			Reg_Num = 1;			
asm("goto	Register_switch_end	");

asm("Register_equal_10_pro:	");
			RegBuff = Reg_FWVersion;      
			I2CDAT = RegBuff[0];		// 提前赋值
			Reg_OP_Mode = REG_OP_READ_WRITE;
			Reg_Num = 4;			
asm("goto	Register_switch_end	");

asm("Register_equal_0x17_pro:	");
			RegBuff = Reg_OffsetData;      
			I2CDAT = RegBuff[0];		// 提前赋值
			Reg_OP_Mode = REG_OP_READ_WRITE;
			Reg_Num = 6;			
asm("goto	Register_switch_end	");

asm("Register_equal_0x21_pro:	");
			RegBuff = Reg_ForceData;
			I2CDAT = RegBuff[0];		// 提前赋值
			Reg_OP_Mode = REG_OP_READ_ONLY;
			Reg_Num = 6;			
asm("goto	Register_switch_end	");

asm("Register_equal_0x22_pro:");
			RegBuff = Reg_AdcData;
			I2CDAT = RegBuff[0];		// 提前赋值
			Reg_OP_Mode = REG_OP_READ_ONLY;
			Reg_Num = 6;			
asm("goto	Register_switch_end	");

asm("Register_equal_0x24_pro:	");
			RegBuff = Reg_ForceData;
			I2CDAT = RegBuff[0];		// 提前赋值
			Reg_OP_Mode = REG_OP_READ_ONLY;
			Reg_Num = 6;			
asm("goto	Register_switch_end	");



asm("Register_equal_0x5b_pro:	");
			RegBuff = Reg_ButtonForceTri;
			I2CDAT = RegBuff[0];		// 提前赋值
			Reg_OP_Mode = REG_OP_READ_WRITE;
			Reg_Num = 10;			
asm("goto	Register_switch_end	");



asm("Register_equal_60_pro:	");
			RegBuff = Reg_DebugModeB;  
			I2CDAT = RegBuff[0];		// 提前赋值
			Reg_OP_Mode = REG_OP_READ_WRITE;
			Reg_Num = 1+1+REG_DEBUGDATA_LENGTH;			//	 允许操作到ready和debug data 寄存器	
			
asm("goto	Register_switch_end	");

asm("Register_equal_61_pro:	");
			RegBuff = Reg_DataReadyB; 
			I2CDAT = RegBuff[0];		// 提前赋值
			Reg_OP_Mode = REG_OP_READ_WRITE;
			Reg_Num = 1+REG_DEBUGDATA_LENGTH;			//	 允许操作到debug data 寄存器
			
			
asm("goto	Register_switch_end	");

asm("Register_equal_62_pro:	");
			RegBuff = Reg_DebugDataB;
			I2CDAT = RegBuff[0];		// 提前赋值
			Reg_OP_Mode = REG_OP_READ_WRITE;
			Reg_Num = REG_DEBUGDATA_LENGTH;			
asm("goto	Register_switch_end	");


asm("Register_switch_none:	");		
        	RegBuff = Reg_WRBuff;
        	I2CDAT = RegBuff[0];		// 提前赋值
			Reg_OP_Mode = REG_OP_READ_ONLY;
			Reg_Num = 4;				
			
asm("Register_switch_end:	");			
				


		TXE = 1;					//M68
				I2CDAT = RegBuff[0];		// 提前赋值		
				I2C_rPointer = 1;
				I2C_TIF = 0;
				I2C_wPointer = 0;	
				flag_IIC_RWAddr = 2;
			}
			else	// 接收数据
			{		
				IIC_Data = I2C_W_Buff ;		
				
				if(Reg_OP_Mode != REG_OP_READ_ONLY)
				{
					if(I2C_wPointer < Reg_Num)
					{
						RegBuff[I2C_wPointer] = IIC_Data; 
						I2C_wPointer++;	
					}
				}
				I2C_rPointer = 0;
				
				//
				I2CDAT = 0X88;//
				I2C_TIF = 0;
			}			
		}
		else//iic wake up 模式
		{
			AWK_EN = 0;
			flag_IntoSleep = 0;
		
		}
		
		gPmuIntoSleepDisable = DEEP_SLEEP_CYCLE_TIME; // 200周期不进入“sleep”指令
	}
	else if(I2C_TIF)	//IIC 发送数据处理
	{	

		IIC_Data = RegBuff[I2C_rPointer]; 
		I2CDAT = IIC_Data;
		I2C_rPointer++;
		
		I2C_TIF = 0;
	}		
	if(I2C_STIF)	// I2C start singal interrput
	{
		flag_IIC_RWAddr = 0;
		I2C_STIF = 0;
	}

//-------------------------------------------------------------------------------------
	// Timer0
	if(TM0IF)
	{
		gPmuPeriodTimeOut = 1;
		TM0IF = 0;
	}
//	
	if(TM1IF)
	{
		TM1IF = 0;
		gTime_Sec++;	
	}
//-------------------------------------------------------------------------------------
	// AFE

	if(ADIF)
	{
		AFE_sample_finish++;
		ADIF = 0;
	}
}

